import java.util.Stack;

public class Test {

    //逆波兰表达式求值
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s : tokens) {
            if(!isOperations(s)) {
                //数字
                stack.push(Integer.valueOf(s));
            } else {
                //运算符
                int val2 = stack.pop();
                int val1 = stack.pop();
                switch(s) {
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    private boolean isOperations(String s) {
        if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
            return true;
        }
        return false;
    }

    //栈的压入、弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i<pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.empty() && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

    //括号的匹配 有效的括号
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        //遍历字符串
        for(int i = 0; i<s.length(); i++){
            char ch = s.charAt(i);

            //把左括号放到栈中
            if(ch == '(' || ch == '[' || ch == '{'){
                stack.push(ch);
            }else {
                //如果此时栈为空
                if(stack.empty()){
                    return false;
                }

                char ret = stack.peek();
                if(ret == '(' && ch == ')' || ret == '[' && ch == ']' || ret == '{' && ch == '}'){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }

        if(!stack.empty()){
            return false;
        }

        return true;
    }

    public static void main2(String[] args) {
        String s = "()";
        boolean flg = isValid(s);
        System.out.println(flg);
    }
    public static void main1(String[] args) {
        Stack stack = new Stack();
        stack.push(1);
        stack.push(2);
        stack.push(3);

        stack.pop();
        stack.pop();

        stack.peek();
        System.out.println(stack);
    }


}
